import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_ohos/app/routes/app_pages.dart';
import 'package:flutter_ohos/app/routes/app_routes.dart';
import 'package:get/get.dart';
import 'package:logging/logging.dart';
import 'package:provider/provider.dart';

import 'app/common/fold_status_provider.dart';
import 'app/common/global.dart';
import 'app/utils/channel_util.dart';


Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Logger.root.level = Level.ALL; // defaults to Level.INFO
  Logger.root.onRecord.listen((record) {
    print('${record.level.name}: ${record.time}: ${record.message}');
  });
  // await Global().init();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  @override
  void initState() {
    // 监听生命周期
    WidgetsBinding.instance.addObserver(this);
    super.initState();

    ///监听原生侧channel
    ChannelUtil.methodChannel.setMethodCallHandler((call) =>
        Future(() {
          if (call.method == 'windowSize') {
            var curBp = call.arguments["breakPoint"];
            if (Global.curBreakPoint != curBp) {
              Global.curBreakPoint = curBp;

              /// 刷新
              setState(() {

              });
            }
          }
        }));
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.inactive: // 处于这种状态的应用程序应该假设它们可能在任何时候暂停。
      // print('处于这种状态的应用程序应该假设它们可能在任何时候暂停');
        break;
      case AppLifecycleState.resumed: // 应用程序可见，前台
      // print('应用程序可见，前台');
        break;
      case AppLifecycleState.paused: // 应用程序不可见，后台
      // print('应用程序不可见，后台');
        break;
      case AppLifecycleState.detached: // 申请将暂时暂停
      // print('申请将暂时暂停');
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return
      ChangeNotifierProvider<FoldStatusProvider>(
        create: (_) => FoldStatusProvider()..init(),
        child: GetMaterialApp(
          title: "测试项目",
          theme: ThemeData(
              primarySwatch: Colors.blue
          ),
          initialRoute: Routes.INDEX,
          getPages: AppPages.routes,
          debugShowCheckedModeBanner: false,
          builder: EasyLoading.init(),
        ),
      );
  }

}
